<?php /* Copyright 2010 Karl R. Wilcox

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License. */


function makeTreatment ( $node ) {

  $treatment_data = array (
    'checky'    => array( 200, 200, '<path d="M0,0h100v100h-100z M100,100h100v100h-100z"  />' ),
    'checky45' => array ( 141.42, 141.42, '<path d="M70.71,0L141.42,70.71 70.71,141.42 0,70.71Z"  />' ),
    'compony' => array ( 333, 300, '<path d="M0,0 h167v300h-167z M166,300 h167v300h-167z"/>' ),
    'crusilly' => array( 200, 200, '  <path d="M 100,25 A 25,25 0 0 0 125,50 25,25 0 0 0 100,75 25,25 0 0 0 75,50 25,25 0 0 0 100,25" />
        <path d="m 97,50 0,50 6,0 0,-50 -6,0 z" />
        <path d="m 175,100 a 25,25 0 0 0 -25,25 25,25 0 0 0 -25,-25 25,25 0 0 0 25,-25 25,25 0 0 0 25,25" />
        <path d="m 100,97 0,6 50,0 0,-6 -50,0 z" />
        <path d="m 100,175 a 25,25 0 0 0 -25,-25 25,25 0 0 0 25,-25 25,25 0 0 0 25,25 25,25 0 0 0 -25,25"/>
        <path d="m 97,100 0,50 6,0 0,-50 -6,0 z"/>
        <path d="M 25,100 A 25,25 0 0 0 50,75 25,25 0 0 0 75,100 25,25 0 0 0 50,125 25,25 0 0 0 25,100"/>
        <path d="m 50,97 0,6 50,0 0,-6 -50,0 z"/>
        <path d="M 125,0 A 25,25 0 0 0 150,-25 25,25 0 0 0 175,0 25,25 0 0 0 150,25 25,25 0 0 0 125,0"/>
        <path d="m 150,-3 0,6 50,0 0,-6 -50,0 z"/>
        <path d="M 200,75 A 25,25 0 0 0 175,50 25,25 0 0 0 200,25 25,25 0 0 0 225,50 25,25 0 0 0 200,75"/>
        <path d="m 197,0 0,50 6,0 0,-50 -6,0 z"/>
        <path d="M 75,0 A 25,25 0 0 0 50,25 25,25 0 0 0 25,0 25,25 0 0 0 50,-25 25,25 0 0 0 75,0"/>
        <path d="M 0,-3 0,3 50,3 50,-3 0,-3 z"/>
        <path d="M 0,75 A 25,25 0 0 0 -25,50 25,25 0 0 0 0,25 25,25 0 0 0 25,50 25,25 0 0 0 0,75"/>
        <path d="M -3,0 -3,50 3,50 3,0 -3,0 z"/>
        <path d="M 0,125 A 25,25 0 0 0 25,150 25,25 0 0 0 0,175 25,25 0 0 0 -25,150 25,25 0 0 0 0,125"/>
        <path d="m -3,150 0,50 6,0 0,-50 -6,0 z"/>
        <path d="M 75,200 A 25,25 0 0 0 50,225 25,25 0 0 0 25,200 25,25 0 0 0 50,175 25,25 0 0 0 75,200" />
        <path d="m 0,197 0,6 50,0 0,-6 -50,0 z" />
        <path d="m 200,125 a 25,25 0 0 0 25,25 25,25 0 0 0 -25,25 25,25 0 0 0 -25,-25 25,25 0 0 0 25,-25" />
        <path d="m 197,150 0,50 6,0 0,-50 -6,0 z" />
        <path d="m 125,200 a 25,25 0 0 0 25,-25 25,25 0 0 0 25,25 25,25 0 0 0 -25,25 25,25 0 0 0 -25,-25" />
        <path d="m 150,197 0,6 50,0 0,-6 -50,0 z" />' ),
    'fretty' => array ( 200, 200, '<polygon points="0,20 76,96 96,76 20,0 0,0" />
        <polygon points="180,200 104,124 124,104 200,180 200,200" />
        <polygon points="4,176 176,4 196,24 24,196" />
        <polygon points="0,180 0,200 20,200" />
        <polygon points="180,0 200,0 200,20" />' ),
    'grillage' => array ( 200, 200, '<polygon points="80,0 100,0 100,76 80,76" />
        <polygon points="80,104 100,104 100,200 80,200" />
        <polygon points="180,4 200,4 200,176 180,176" />
        <polygon points="4,80 4,100 176,100 176,80" />
        <polygon points="0,180 0,200 76,200 76,180" />
        <polygon points="104,180 104,200 200,200 200,180" />' ),
    'honeycombed' => array ( 300, 347,
         '<path d="m 153.4422,92.600196 -46.21319,80.043594 45.12205,81.13837 95.02852,1.00289 46.35761,-82.54424 -45.99123,-79.659144 -94.30376,0.01853 z m -6.68594,-10.028905 -52.060166,90.170859 53.401696,91.51979 105.30491,0 52.25179,-90.1727 -53.28385,-92.224757 -105.61438,0.706808 z" fill-rule="evenodd" />
          <path d="M 3.4422,6.600196 -42.77099,86.64379 2.35106,167.78216 97.37958,168.78505 143.73719,86.24081 97.74596,6.581666 3.4422,6.600196 z m -6.68594,-10.028905 -52.060166,90.170859 53.401696,91.51979 105.30491,0 52.25179,-90.1727 -53.28385,-92.224757 -105.61438,0.706808 z" fill-rule="evenodd" />
          <path d="m 3.4422,180.6002 -46.21319,80.04359 45.12205,81.13837 95.02852,1.00289 46.35761,-82.54424 -45.99123,-79.65914 -94.30376,0.0185 z m -6.68594,-10.02891 -52.060166,90.17086 53.401696,91.51979 105.30491,0 52.25179,-90.1727 -53.28385,-92.22476 -105.61438,0.70681 z" fill-rule="evenodd" />
          <path d="m 303.4422,8.600196 -46.21319,80.043594 45.12205,81.13837 95.02852,1.00289 46.35761,-82.54424 -45.99123,-79.659144 -94.30376,0.01853 z m -6.68594,-10.028905 -52.06017,90.170859 53.4017,91.51979 105.30491,0 52.25179,-90.1727 -53.28385,-92.224757 -105.61438,0.706808 z" fill-rule="evenodd" />
          <path d="m 303.4422,178.6002 -46.21319,80.04359 45.12205,81.13837 95.02852,1.00289 46.35761,-82.54424 -45.99123,-79.65914 -94.30376,0.0185 z m -6.68594,-10.02891 -52.06017,90.17086 53.4017,91.51979 105.30491,0 52.25179,-90.1727 -53.28385,-92.22476 -105.61438,0.70681 z" fill-rule="evenodd" />
         '),
    'maily' => array ( 100, 100,
         '<path d="m 2.124176,-59.94799 c -19.256231,-0.65144 -38.51525,7.93755 -50.670736,24.66794 -19.448777,26.76862 -13.488434,64.36039 13.280512,83.80893 26.768945,19.44854 64.361178,13.5193 83.809955,-13.24932 L 42.120856,30.59421 C 25.195428,53.88978 -7.2847804,59.03113 -30.580634,42.10591 -53.876488,25.18069 -59.048937,-7.29912 -42.123509,-30.59469 -25.19808,-53.89026 7.3131568,-59.03162 30.609011,-42.1064 l 0.124117,0.0931 c 16.874227,12.33677 24.727375,33.53658 19.951796,53.89712 l 7.726279,1.8307 C 63.92381,-9.78832 54.793836,-34.33985 35.263396,-48.52937 25.225041,-55.82257 13.677915,-59.55712 2.124176,-59.94799 z" />
          <path d="M 159.90246,2.12025 C 160.5539,-17.13599 151.96491,-36.395 135.23452,-48.55049 108.4659,-67.99927 70.874126,-62.03893 51.425585,-35.26998 31.977045,-8.50104 37.906287,29.0912 64.674906,48.53998 l 4.685357,-6.42306 C 46.064693,25.1915 40.923336,-7.28871 57.848558,-30.58457 c 16.925222,-23.29585 49.405032,-28.4683 72.700602,-11.54287 23.29557,16.92543 28.43693,49.43667 11.51171,72.73252 l -0.0931,0.12412 C 129.631,47.60342 108.43119,55.45657 88.070652,50.68099 l -1.830702,7.72628 c 23.50283,5.51261 48.05436,-3.61737 62.24388,-23.14781 7.2932,-10.03835 11.02775,-21.58548 11.41862,-33.13921 z" />
          <path d="m 97.834225,159.89852 c 19.256235,0.65144 38.515245,-7.93755 50.670735,-24.66794 C 167.95374,108.46196 161.9934,70.87019 135.22445,51.42165 108.45551,31.9731 70.863272,37.90235 51.414495,64.67097 l 6.423051,4.68535 C 74.762974,46.06075 107.24318,40.9194 130.53904,57.84462 c 23.29585,16.92522 28.4683,49.40503 11.54287,72.7006 -16.92543,23.29557 -49.436666,28.43693 -72.73252,11.51171 l -0.124117,-0.0931 C 52.351046,129.62707 44.497898,108.42726 49.273478,88.06672 l -7.726279,-1.8307 c -5.512608,23.50283 3.617366,48.05436 23.147806,62.24388 10.038355,7.2932 21.585481,11.02775 33.13922,11.41862 z" />
          <path d="m -59.944058,97.83028 c -0.651442,19.25624 7.937552,38.51525 24.667939,50.67074 26.76862,19.44878 64.360395,13.48844 83.808936,-13.28051 19.44854,-26.76894 13.519299,-64.36118 -13.249321,-83.80996 l -4.685357,6.42306 c 23.29557,16.92542 28.436927,49.40563 11.511705,72.70149 -16.925222,23.29585 -49.4050352,28.4683 -72.700606,11.54287 -23.29557,-16.92543 -28.436927,-49.43667 -11.511705,-72.73252 l 0.09309,-0.12412 C -29.672608,52.34711 -8.472792,44.49396 11.887743,49.26954 l 1.830702,-7.72628 c -23.5028342,-5.51261 -48.05436,3.61737 -62.243882,23.14781 -7.293202,10.03835 -11.027753,21.58548 -11.418618,33.13921 z" />
         ' ),
    'masoned' => array ( 200, 140, '<polygon points="0,0 20,0 20,50 200,50 200,70 110,70 110,120 200,120 200,140 0,140 0,120 90,120 90,70 0,70 " />' ),
    'papelonny' => array ( 100, 100, '<path d="M 50,0 A 50 50 0 0,1 0,50 A 50 50 0 0,0 100,50 A 50 50 0 0,1 50,0" />' ),
    'scaly' => array ( 100, 100, '<path d="M 0,0 A 50 30 0 0,0 100,0 A 50 50 0 0,1 0,0" />
        <path d="M -50,50 A 50,30 0 0 0 50,50 50,50 0 0 1 -50,50" />
        <path d="M 50,50 A 50,30 0 0 0 150,50 50,50 0 0 1 50,50" />' ),
    'vairy' => array ( 125, 240, '<polygon points="62.5,0 96.5,20 96.5,100 125,120 0,120 33,100 33,20" />
        <polygon points="125,120 159,140 159,220 187.5,240 62.5,240 95.5,220 95.5,140" />
        <polygon points="0,120 34,140 34,220 62.5,240 -62.5,240 -29.5,220 -29.5,140" />' ),
  );

  $name = $node->getAttribute('name');
  $parent = $node->parentNode;
  // Some treatments are rotated if on some ordinaries
  if ( $parent->nodeName == 'ordinary' ) {
    $ord_type = $parent->getAttribute('subtype');
    if ( $ord_type == 'saltire' or substr($ord_type,0,6) == 'chevron' ) {
      if ( array_key_exists($name . '45', $treatment_data) ) $name .= '45';
    }
  }

  $children = $node->childNodes;
  $tinctures = array();
  foreach ( $children as $child ) {
    if ( $child->nodeName == 'tincture' )
      $tinctures[$child->getAttribute('index')] = $child;
  }
  if ( array_key_exists($name, $treatment_data) ) {
    $treatment = $treatment_data[$name][2];
    $size_x = $treatment_data[$name][0];
    $size_y = $treatment_data[$name][1];
    $base = apply_tincture ( $tinctures[1], '<rect x="0" y="0" width="' .
                  $size_x . '" height="' . $size_y . '" />', "$size_x,$size_y" );
    $pattern = apply_tincture ( $tinctures[2], $treatment, "$size_x,$size_y" );
    return array ( $size_x, $size_y, $base . $pattern );
  } else {
    draw_message('internal', "Cannot draw treatment $name", __FILE__, __LINE__);
    return array ( 100, 100, '<rect x="0" y="0" width="100" height="100" fill="#888888" />' );
  }
}

function makeSemy( $node ) {
  $name = $node->getAttribute('name');
  $children = $node->childNodes;
  $background = $charge = null;
  foreach ( $children as $child ) {
    if ( $child->nodeName == 'tincture' )
      $background = $child;
   elseif ( $child->nodeName == 'charge' )
     $charge = $child;
  }
  if ( $background == null or $charge == null )
    draw_message('internal', 'Bad semyde element', __FILE__, __LINE__ );
 if ( strpos ( $name, 'counter-' ) === 0 ) { // See rewriter.inc, some treatments are rewritten as semyde
    // special layout
    $semy = PlaceCharge($charge, 100, 100, 50, 50, 90);
    $semy .= PlaceCharge($charge, 100, 100, 200, 50, 90);
    $semy .= PlaceCharge($charge, 100, 100, 25, 175);
    $semy .= PlaceCharge($charge, 100, 100, 125, 175);
    $semy .= PlaceCharge($charge, 100, 100, 225, 175);
    $base = apply_tincture ( $background, '<rect x="0" y="0" width="300" height="250" />' );
    return array ( 300, 250, $base . $semy );
  } else {
    $semy = PlaceCharge($charge, 100, 100, 50, 50);
    $semy .= PlaceCharge($charge, 100, 100, 150, 150);
    $base = apply_tincture ( $background, '<rect x="0" y="0" width="200" height="200" />' );
    return array ( 200, 200, $base . $semy );
 }
}
?>